﻿using System;
using System.Collections.Generic;
using System.Text;

namespace EasyFramework.Utility
{
    /// <summary>
    /// 版权：广联达软件股份有限公司
    /// 创建：战东耀, 2012-04-19
    /// 说明: 格式化帮助类
    /// 修改: 
    /// </summary>
    public class FormatHelper
    {
        /// <summary>
        ///文件大小格式化
        /// </summary>
        /// <param name="fileSize"></param>
        /// <returns></returns>
        public static String FormatFileSize(Int64 fileSize)
        {
            if (fileSize < 0)
            {
                throw new ArgumentOutOfRangeException("fileSize");
            }
            else if (fileSize >= 1024 * 1024 * 1024)
            {
                return string.Format("{0:########0.00} GB", ((Double)fileSize) / (1024 * 1024 * 1024));
            }
            else if (fileSize >= 1024 * 1024)
            {
                return string.Format("{0:####0.00} MB", ((Double)fileSize) / (1024 * 1024));
            }
            else if (fileSize >= 1024)
            {
                return string.Format("{0:####0.00} KB", ((Double)fileSize) / 1024);
            }
            else
            {
                return string.Format("{0} bytes", fileSize);
            }
        }

        /// <summary>
        /// 时间提示格式化
        /// </summary>
        /// <param name="longtime"></param>
        /// <returns></returns>
        public static string FormatTimeNote(DateTime longtime)
        {
            TimeSpan ts = DateTime.Now - longtime;
            if (ts >= new TimeSpan(1, 0, 0, 0) && ts.TotalDays < 6)
            {
                return String.Format("{0:N0}天以前", ts.TotalDays);
            }
            else if (ts >= new TimeSpan(1, 0, 0, 0) && ts.TotalDays >= 6)
            {
                return longtime.ToLongDateString();
            }
            else if (ts >= new TimeSpan(1, 0, 0))
            {
                return String.Format("{0:N0}小时以前", ts.TotalHours);
            }
            else if (ts >= new TimeSpan(0, 1, 0))
            {
                return String.Format("{0:N0}分钟以前", ts.TotalMinutes);
            }
            else
            {
                return "刚才";
            }
        }
    }
}
